package com.sihan.swvp.action.worksy;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.worksy.Worksy;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.worksy.TaskImpService;
import com.sihan.swvp.service.worksy.WorksyService;


@Controller
@RequestMapping("/worksy")
public class WorksyAction extends BaseAction{

	@Autowired
	private WorksyService service;
	
	@Autowired
	private LogService logService;
	@Autowired
	private TaskImpService impService;

	@ResponseBody
	@RequestMapping("/list")
	public Object getList(Integer page,Integer limit){
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("bdz", getParameter("bdz"));
		kv.put("bz", getParameter("bz"));
		kv.put("zy", getParameter("zy"));
		kv.put("fStage",getParameter("fStage")); //完成阶段
		kv.put("powercut",getParameter("powercut"));//停电范围
		kv.put("startTime",getParameter("startTime"));//计划开始时间
		kv.put("endTime",getParameter("endTime"));//计划结束时间
		kv.put("content", getParameter("content"));//任務內容
		kv.put("planStime",getParameter("planStime"));//编制开始时间
		kv.put("planEtime",getParameter("planEtime"));//编制结束时间
		kv.put("status", getParameter("status"));//是否完成
		
		return pageData(service.getList(page, limit,kv));
	}

	@ResponseBody
	@RequestMapping("/edit")
	public Object editWorksy(Worksy worksy){
		try {
			worksy.setUpuser(getLoginUser().getId());
			worksy.setStarttime(CommonUtil.o2date(getParameter("starttime")));
			worksy.setEndtime(CommonUtil.o2date(getParameter("endtime")));
//			if(worksy.getId() == null){
//				VWorksy vWorksy = new VWorksy();
//				BeanUtils.copyProperties(worksy, vWorksy);
//				List<Map<String, Object>> list = service.inspectSameTask(vWorksy);
//				if(list.size() > 0 && !list.isEmpty()){
//					return successMsg("系统中存在此任务。");
//				}
//			}
			service.addWorksy(worksy);
			if(worksy.getId() != null && !"".equals(worksy.getId())){
				logService.addUserLog("修改工作任务", "工作任务", getLoginUser().getId(), "修改工作任务："+worksy.getContent(),getRemoteAddr(),1,0);
			}else{
				logService.addUserLog("添加工作任务", "工作任务", getLoginUser().getId(), "添加工作任务："+worksy.getContent(),getRemoteAddr(),1,0);
			}
			return successMsg("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			if(worksy.getId() != null && !"".equals(worksy.getId())){
				logService.addUserLog("修改工作任务", "工作任务", getLoginUser().getId(), "修改工作任务："+worksy.getContent(),getRemoteAddr(),0,0);
			}else{
				logService.addUserLog("添加工作任务", "工作任务", getLoginUser().getId(), "添加工作任务："+worksy.getContent(),getRemoteAddr(),0,0);
			}
			return failMsg("保存失败！");
		} 
	}
	
//	@ResponseBody
//	@RequestMapping("/inspectSameTask")
//	public Object inspectSameTask(VWorksy vWorksy){
//		List<Map<String, Object>> list = service.inspectSameTask(vWorksy);
//		return list;
//	}

	@ResponseBody
	@RequestMapping("/del")
	public Object delById(String id,String content){
		try {
			service.delById(id);
			
			logService.addUserLog("删除工作任务", "工作任务", getLoginUser().getId(), "删除工作任务："+content,getRemoteAddr(),1,0);
			return successMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除工作任务", "工作任务", getLoginUser().getId(), "删除工作任务："+content,getRemoteAddr(),0,0);
			return failMsg("删除失败！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/getBdz")
	public List<Map<String, Object>> getBdz(Integer regionId){
		
		return service.getBdz(regionId);
	}
	
	@ResponseBody
	@RequestMapping("/getBz")
	public List<Map<String, Object>> getBz(Integer zyId,Integer bzId){
		
		return service.getBz(zyId,bzId);
	}
	
	@ResponseBody
	@RequestMapping("/getzy")
	public List<Map<String, Object>> getZy(){
		
		return service.getZy();
	}
	
	@ResponseBody
	@RequestMapping("/getDept")
	public List<Map<String, Object>> getDept(){
		
		return service.getDept();
	}
	
	@ResponseBody
	@RequestMapping("/getDeptPeople")
	public List<Map<String, Object>> getDeptPeople(int deptId){
		
		return service.getDeptPeople(deptId);
	}
	
	@ResponseBody
	@RequestMapping("/getCity")
	public List<Map<String, Object>> getCity(){
		
		return service.getCity();
	}
	
	@RequestMapping("/impExcel")
	protected void impExcel(@RequestParam("file")MultipartFile file,HttpServletResponse response){
		ServletOutputStream output = null;
		Object msg = null;
		try {
			response.setContentType("text/html;charset=UTF-8");
			output = response.getOutputStream();
			String name = file.getOriginalFilename();
			String fileType = name.substring(name.lastIndexOf('.'));
			if(!(".xls".equals(fileType) || ".xlsx".equals(fileType))){
				msg = failMsg("文件格式错误，请上传Excel文件。");
				return ;
			}
			try {
				impService.addWorkImp(file, getLoginUser().getId());
				msg = successMsg("导入数据成功。");
				return ;
			} catch (Exception e) {
				logService.addUserLog("任务导入","工作任务导入。", 
						Integer.valueOf(getLoginUser().getId().toString()), e.getMessage(),getRemoteAddr(),0,0);
				msg = failMsg("服务器异常");
				return ;

			}
			
			
		} catch (Exception e) {
			msg = failMsg("错误，请检查文件数据格式是否正确。");
			return ;
		}
		finally{
			try {
				output.write(JSON.toJSONString(msg).getBytes("utf-8"));
				output.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
			CommonUtil.close(output);
		}
	}
	
}
